package com.billing.web.controller.business;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.User;
import org.bouncycastle.jce.provider.JCEBlockCipher.TEA;
import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression;
import org.quartz.utils.StringKeyDirtyFlagMap;
import org.springframework.cache.interceptor.NameMatchCacheOperationSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestScope;

import com.billing.model.entity.TDept;
import com.billing.model.entity.TOrganization;
import com.billing.model.entity.TRoleMembers;
import com.billing.service.DeptService;
import com.billing.service.MaindataService;
import com.billing.service.OrganizationService;
import com.billing.service.RoleMembersService;
import com.billing.service.UserBeanService;
import com.billing.service.UserService;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.mchange.v2.c3p0.test.StatsTest;
@Controller
@RequestMapping("organization")
public class OrganizationController {
	
	@Resource
	private OrganizationService organizationService;
	@Resource
	private UserService  userService;
	@Resource
	private UserBeanService userBeanService;
	@Resource
	private RoleMembersService roleMembersService;
	@Resource
	private DeptService deptService;
	
	/**
	 * 获取企业列表信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="index", method = RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		String status = request.getParameter("status");
		String name = request.getParameter("name");
		if (name != null && name != "") {
			try {
				name = URLDecoder.decode(URLDecoder.decode(name, "UTF-8"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		int currentPage = 1;
		
		int pageSize = 10;
		request.setAttribute("name", name);
		request.setAttribute("status", status);
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		String username = (String) request.getSession().getAttribute("username");
		int total = organizationService.getTORganizationNum(username);
		int totalPage = total / pageSize;
		if (total % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		int s = 0;
		if (status != null && status != "") {
			 s = Integer.parseInt(status);
		}
		List<TOrganization> organizations = organizationService.getTOrganizationByName(name,s,username,pageSize,(currentPage - 1) * pageSize);
		request.setAttribute("totalNum", total);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPage", totalPage);
		request.setAttribute("organizations", organizations);
		return "organization/index";
	}
	
	@RequestMapping(value="addOrganization", method = RequestMethod.GET)
	public String add(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		if (id != null && !id.equals("")) {
			int oid = Integer.parseInt(id);
			TOrganization organization = organizationService.getTOrganizationByid(oid);
			request.setAttribute("organization", organization);
		}
		return "organization/addOrganization";
	}
	
	@RequestMapping(value="/save",method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,Model model,TOrganization tOrganization){
		if (tOrganization.getId() != null) {
			//修改操作
			//企业是否存在
			TOrganization tOrganization2 = organizationService.getTOrganizationByid(tOrganization.getId());
			if (tOrganization2.getName()==tOrganization.getName()) {
				return "custNameExist";
			}
			int i = organizationService.updateTOrganization(tOrganization);
			if (i > 0) {
				return "updateok";
			}else{
				return "updateno";
			}
		}else{
			
			String name = request.getParameter("name");
			String code = request.getParameter("code");
			String id = request.getParameter("id");
			String status = request.getParameter("status");
			TOrganization organization = new TOrganization();
			organization.setName(name);
			organization.setCode(code);
			organization.setFatherId(0);
			organization.setNumber(0);
			organization.setSort(0);
			organization.setStatus(Integer.parseInt(status));
			organization.setTop(0);
			int i = organizationService.saveTOraganization(organization);
			if (i < 0) {
				return "addno";
			}else{
				TRoleMembers roleMembers = new TRoleMembers();
				roleMembers.setName("文员");
				roleMembers.setType(2);
				roleMembers.setOrganizationId(organization.getId());
				roleMembers.setSort(0);
				roleMembers.setStatus(1);
				roleMembersService.insert(roleMembers);
				TDept dept = new TDept();
				dept.setName("人事部");
				dept.setOrganizationId(1);
				dept.setFatherId(1);
				dept.setSort(0);
				dept.setStatus(0);
				dept.setTop(1);
				dept.setType(2);
				deptService.insert(dept);
				return "addok";
			}	
		}
	}
	
	@RequestMapping(value="/addRole",method=RequestMethod.GET)
	public String addRole(HttpServletRequest request,Model model){
		String organizationid = request.getParameter("organizationid");
		String id = request.getParameter("id");
		if (id != null && id != "") {
			TRoleMembers roleMembers = roleMembersService.getRole(id);
			request.setAttribute("rolemembers", roleMembers);
		}
		request.setAttribute("organizationid", Integer.parseInt(organizationid));
		return "organization/addRole";
	}
	
	@RequestMapping(value="/saveRole",method=RequestMethod.POST)
	@ResponseBody
	public String saveRole(HttpServletRequest request,Model model,TRoleMembers roleMembers){
		if (roleMembers.getId() != null) {
			//修改
			int i = roleMembersService.updateRole(roleMembers);
			if (i > 0) {
				return "updateok";
			}else{
				return "updateno";
			}
		}else{
			String organizationid = request.getParameter("organizationid");
			roleMembers.setOrganizationId(Integer.parseInt(organizationid));
			roleMembers.setSort(0);
			int i = organizationService.insertMembers(roleMembers);
			if (i > 0){
				return "addok";
			}else{
				return "addno";
			}
		}
	}
	@RequestMapping(value="/checkRole",method=RequestMethod.GET)
	public String checkRole(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		if (name != null && name != "") {
			try {
				name = URLDecoder.decode(URLDecoder.decode(name, "UTF-8"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		String status = request.getParameter("status");
		int s = 0;
		if (status != null && status != "") {
			s = Integer.parseInt(status);
		}
		int pageSize = 10;
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int total = roleMembersService.getroleNum(Integer.parseInt(id),name,s);
		int totalPage = total / pageSize;
		if (total % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<TRoleMembers> roleMembers = roleMembersService.getRoleById(Integer.parseInt(id),name,s,pageSize,(currentPage - 1) * pageSize);
		request.setAttribute("role", roleMembers);
		request.setAttribute("organizationid", id);
		request.setAttribute("name", name);
		request.setAttribute("status", status);
		request.setAttribute("totalNum", total);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPage", totalPage);
		return "organization/role";
		
	}
	
	@RequestMapping(value="dept",method=RequestMethod.GET)
	public String dept(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		if (name != null && name != "") {
			try {
				name = URLDecoder.decode(URLDecoder.decode(name, "UTF-8"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		String status = request.getParameter("status");
		int s = 0;
		if (status != null && status != "") {
			s = Integer.parseInt(status);
		}
		int pageSize = 10;
		int currentPage = 1;
		try {
			currentPage = new Integer(request.getParameter("currentPage"));
		} catch (Exception e) {
		}
		currentPage = currentPage <= 0 ? 1 : currentPage;
		int total = deptService.getDeptNum(Integer.parseInt(id),name,s);
		int totalPage = total / pageSize;
		if (total % pageSize != 0) {
			totalPage++;	
		}
		totalPage = totalPage <= 0 ? 1 : totalPage;
		currentPage = currentPage > totalPage ? totalPage : currentPage;
		List<Map<String, Object>> depts = deptService.getDeptById(Integer.parseInt(id),name,s,pageSize,(currentPage - 1) * pageSize);
		
		request.setAttribute("dept", depts);
		request.setAttribute("id", Integer.parseInt(id));
		request.setAttribute("name", name);
		request.setAttribute("status", status);
		request.setAttribute("totalNum", total);
		request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPage", totalPage);
		
		return "organization/dept";
	}
	
	@RequestMapping(value="addDept",method=RequestMethod.GET)
	public String addDept(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		request.setAttribute("id", Integer.parseInt(id));
		List<TDept> depts = deptService.getAllDept(id);
		request.setAttribute("depts", depts);
		return "organization/addDept";
	}
	@RequestMapping(value="saveDept",method=RequestMethod.POST)
	@ResponseBody
	public String saveDept(HttpServletRequest request,Model model,TDept dept){
		String id = request.getParameter("id");
		dept.setOrganizationId(Integer.parseInt(id));
		dept.setTop(Integer.parseInt(id));
		dept.setSort(1);
		System.out.println(dept);
		int i = deptService.insert(dept);
		if (i > 0) {
			return "addok";
		}else{
			return "addno";
		}
	}
}
